package com.asiainfo.dacp.datastash.worker.executor.adapter.datax.model.plugin;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;

@Data
public class RdbmsWriter implements Serializable{
	private static final long serialVersionUID = -191087038205912210L;
	private String username;
	private String password;

	/**
	 * column配置项必须指定，不能留空！
	 */
	private List<String> column;

	private List<String> postSql;
	private List<String> preSql; 
	
	private int batchSize = 1024;

	private List<Connection> connection;

	public RdbmsWriter(){};
	
	public RdbmsWriter(RdbmsWriter source){
		BeanUtils.copyProperties(source, this);
	}
	 
	public void initDataSource(@NonNull String username, @NonNull String password, @NonNull String jdbcUrl){
		this.setUsername(username);
		this.setPassword(password);
		if(this.connection == null){
			this.setConnection(Lists.newArrayList(new Connection(jdbcUrl)));
		} else{
			connection.get(0).setJdbcUrl(jdbcUrl);
		}
	}
	
	public void setTable(List<String> table){
		connection.get(0).setTable(table);
	}

	@Data 
	private class Connection {
		/**
		 * table 和 jdbcUrl 必须包含在 connection 配置单元中
		 */
		public Connection(String jdbcUrl){
			this.setJdbcUrl(jdbcUrl);
		}
		private String jdbcUrl;
		private List<String> table;

	} 
}
